
const { getDarwinAppList } = require('../../../common/utils/darwinUtil/getAppList');
const { doSearchSysPlugin } = require('./doSearchSysPlugin');
const { doOrderPlugin } = require('./doOrderPlugin');


exports.doSearchByKeyword = async function (request) {
    const keyword = request.input

    let plugins = []
    if (keyword != "") {
        const sysPlugins = doSearchSysPlugin()
        const appPlugin = await getDarwinAppList()
        plugins = [...sysPlugins, ...appPlugin]
    }
    return {
        preSearch: preSearch(request),
        resultList: doOrderAndFilterPLugin(plugins, request)
    }
}


function preSearch(request) {
    const keyword = request.input
    try {
        result = eval(keyword)
        if (typeof result === 'number' && !isNaN(result)) {
            return result
        }
        return null
    } catch (e) {
        return null
    }
}



function doOrderAndFilterPLugin(plugins, request) {
    plugins = filterByKeyword(plugins, null)
    plugins = doOrderPlugin(plugins)
    return plugins
}
